package com.yunwaikeji.gy_tool_library.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.annotation.Nullable;

import com.yunwaikeji.gy_tool_library.R;
import com.yunwaikeji.gy_tool_library.info.GYDisplayInfo;
import com.yunwaikeji.gy_tool_library.utils.display.GYDisplayUtils;
import com.yunwaikeji.gy_tool_library.utils.GYViewUtils;
import com.yunwaikeji.gy_tool_library.view.GYLinearLayout;
import com.yunwaikeji.gy_tool_library.view.GYTextView;

public class GYCheckBox extends GYLinearLayout {
    public GYCheckBox(Context context) {
        this(context, null);
    }

    public GYCheckBox(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GYCheckBox(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public GYCheckBox(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs);
    }

    private GYTextView leftTextView, topTextView, rightTextView, bottomTextView;
    private GYLinearLayout checkBoxLL;
    private CheckBox checkBox;
    private Drawable normalDrawable;//默认的Drawable
    private Drawable checkedDrawable;//选择的Drawable
    private int drawableWidthPx = GYDisplayUtils.dp2px(getContext(), 15);//选择Drawable高
    private int drawableHeightPx = GYDisplayUtils.dp2px(getContext(), 15);//选择Drawable宽
    private boolean clickable = true;//是否可以点击

    private void init(AttributeSet attributeSet) {
        LayoutInflater.from(getContext()).inflate(R.layout.view_gy_check_box, this);
        leftTextView = findViewById(R.id.leftTextView);
        topTextView = findViewById(R.id.topTextView);
        rightTextView = findViewById(R.id.rightTextView);
        bottomTextView = findViewById(R.id.bottomTextView);
        checkBoxLL = findViewById(R.id.checkBoxLL);
        checkBox = findViewById(R.id.checkBox);
        if (attributeSet != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.GYCheckBox);
            setLeftText(typedArray.getString(R.styleable.GYCheckBox_leftText));
            setLeftTextSizePx(typedArray.getDimensionPixelSize(R.styleable.GYCheckBox_leftTextSize, GYDisplayUtils.sp2px(getContext(), 14)));
            setLeftTextColor(typedArray.getColor(R.styleable.GYCheckBox_leftTextColor, Color.BLACK));
            setLeftSpacingPx(typedArray.getDimensionPixelSize(R.styleable.GYCheckBox_leftSpacing, GYDisplayUtils.dp2px(getContext(), 3)));
            setTopText(typedArray.getString(R.styleable.GYCheckBox_topText));
            setTopTextSizePx(typedArray.getDimensionPixelSize(R.styleable.GYCheckBox_topTextSize, GYDisplayUtils.sp2px(getContext(), 14)));
            setTopTextColor(typedArray.getColor(R.styleable.GYCheckBox_topTextColor, Color.BLACK));
            setTopSpacingPx(typedArray.getDimensionPixelSize(R.styleable.GYCheckBox_topSpacing, GYDisplayUtils.dp2px(getContext(), 3)));
            setRightText(typedArray.getString(R.styleable.GYCheckBox_rightText));
            setRightTextSizePx(typedArray.getDimensionPixelSize(R.styleable.GYCheckBox_rightTextSize, GYDisplayUtils.sp2px(getContext(), 14)));
            setRightTextColor(typedArray.getColor(R.styleable.GYCheckBox_rightTextColor, Color.BLACK));
            setRightSpacingPx(typedArray.getDimensionPixelSize(R.styleable.GYCheckBox_rightSpacing, GYDisplayUtils.dp2px(getContext(), 3)));
            setBottomText(typedArray.getString(R.styleable.GYCheckBox_bottomText));
            setBottomTextSizePx(typedArray.getDimensionPixelSize(R.styleable.GYCheckBox_bottomTextSize, GYDisplayUtils.sp2px(getContext(), 14)));
            setBottomTextColor(typedArray.getColor(R.styleable.GYCheckBox_bottomTextColor, Color.BLACK));
            setBottomSpacingPx(typedArray.getDimensionPixelSize(R.styleable.GYCheckBox_bottomSpacing, GYDisplayUtils.dp2px(getContext(), 3)));
            int textSize = typedArray.getDimensionPixelSize(R.styleable.GYCheckBox_leftTextSize, WRAP_CONTENT);
            if (textSize != WRAP_CONTENT) {
                setTextSizePx(textSize);
            }
            int textColor = typedArray.getColor(R.styleable.GYCheckBox_textColor, WRAP_CONTENT);
            if (textColor != WRAP_CONTENT) {
                setTextColor(textColor);
            }
            int spacing = typedArray.getDimensionPixelSize(R.styleable.GYCheckBox_spacing, WRAP_CONTENT);
            if (spacing != WRAP_CONTENT) {
                setSpacingPx(spacing);
            }
            Drawable checkBoxBackground = typedArray.getDrawable(R.styleable.GYCheckBox_checkBoxBackground);
            setCheckBoxBackground(checkBoxBackground);
            int checkBoxWidthPx = typedArray.getDimensionPixelSize(R.styleable.GYCheckBox_checkBoxWidth, GYDisplayUtils.dp2px(getContext(), 15));
            int checkBoxHeightPx = typedArray.getDimensionPixelSize(R.styleable.GYCheckBox_checkBoxHeight, GYDisplayUtils.dp2px(getContext(), 15));
            setCheckBoxSizePx(checkBoxWidthPx, checkBoxHeightPx);
            int checkBoxRadiusPx = typedArray.getDimensionPixelSize(R.styleable.GYCheckBox_checkBoxRadius, 0);
            setCheckBoxRadiusPx(checkBoxRadiusPx);
            int checkBoxEdgeWidthPx = typedArray.getDimensionPixelSize(R.styleable.GYCheckBox_checkBoxEdgeWidth, 0);
            setCheckBoxEdgeWidthPx(checkBoxEdgeWidthPx);
            int checkBoxEdgeColor = typedArray.getColor(R.styleable.GYCheckBox_checkBoxEdgeColor, Color.TRANSPARENT);
            setCheckBoxEdgeColor(checkBoxEdgeColor);
            normalDrawable = typedArray.getDrawable(R.styleable.GYCheckBox_normalDrawable);
            checkedDrawable = typedArray.getDrawable(R.styleable.GYCheckBox_checkedDrawable);
            drawableWidthPx = typedArray.getDimensionPixelSize(R.styleable.GYCheckBox_drawableWidth, drawableWidthPx);
            drawableHeightPx = typedArray.getDimensionPixelSize(R.styleable.GYCheckBox_drawableHeight, drawableHeightPx);
            setChecked(typedArray.getBoolean(R.styleable.GYCheckBox_checked, false));
            clickable = typedArray.getBoolean(R.styleable.GYCheckBox_clickable, clickable);
            setLeftTextBold(typedArray.getBoolean(R.styleable.GYCheckBox_leftTextBold, false));
            setTopTextBold(typedArray.getBoolean(R.styleable.GYCheckBox_topTextBold, false));
            setRightTextBold(typedArray.getBoolean(R.styleable.GYCheckBox_rightTextBold, false));
            setBottomTextBold(typedArray.getBoolean(R.styleable.GYCheckBox_bottomTextBold, false));
            typedArray.recycle();
        }
        if (normalDrawable == null) {
            normalDrawable = getResources().getDrawable(R.drawable.gylibrary_check_normal);
        }
        if (checkedDrawable == null) {
            checkedDrawable = getResources().getDrawable(R.drawable.gylibrary_checked);
        }
        refreshCheckBox();
        setOnClickListener(v -> checkBox.setChecked(!checkBox.isChecked()));
        setClickable(clickable);
    }

    @Override
    public void setClickable(boolean clickable) {
        super.setClickable(clickable);
    }

    public GYCheckBox setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener onCheckedChangeListener) {
        checkBox.setOnCheckedChangeListener(onCheckedChangeListener);
        return this;
    }

    public GYCheckBox setNormalDrawable(Drawable normalDrawable) {
        this.normalDrawable = normalDrawable;
        return refreshCheckBox();
    }

    public GYCheckBox setCheckedDrawable(Drawable checkedDrawable) {
        this.checkedDrawable = checkedDrawable;
        return refreshCheckBox();
    }

    public GYCheckBox setDrawableWidthPx(int drawableWidthPx) {
        this.drawableWidthPx = drawableWidthPx;
        return refreshCheckBox();
    }

    public GYCheckBox setDrawableHeightPx(int drawableHeightPx) {
        this.drawableHeightPx = drawableHeightPx;
        return refreshCheckBox();
    }

    private GYCheckBox refreshCheckBox() {
        GYViewUtils.setSize().setSizePx(checkBox, drawableWidthPx, drawableHeightPx);
        checkBox.setBackground(GYDisplayUtils.newBuilder()
                .setSizePx(drawableWidthPx, drawableHeightPx)
                .setNormal(normalDrawable)
                .setChange(checkedDrawable)
                .setChangeEffect(GYDisplayInfo.ChangeEffect.CHECK)
                .buildDrawable());
        return this;
    }

    public GYCheckBox setCheckBoxBackground(Drawable checkBoxBackground) {
        checkBoxLL.getBackgroundBuilder().setBackground(checkBoxBackground).build();
        return this;
    }

    public GYCheckBox setCheckBoxSizePx(int checkBoxWidthPx, int checkBoxHeightPx) {
        checkBoxLL.getViewSet().setSizePx(checkBoxWidthPx, checkBoxHeightPx);
        return this;
    }

    public GYCheckBox setCheckBoxSizeDp(int checkBoxWidthDp, int checkBoxHeightDp) {
        return setCheckBoxSizePx(GYDisplayUtils.dp2px(getContext(), checkBoxWidthDp), GYDisplayUtils.dp2px(getContext(), checkBoxHeightDp));
    }

    public GYCheckBox setCheckBoxWidthPx(int checkBoxWidthPx) {
        checkBoxLL.getViewSet().setWidthPx(checkBoxWidthPx);
        return this;
    }

    public GYCheckBox setCheckBoxWidthDp(int checkBoxWidthDp) {
        return setCheckBoxWidthPx(GYDisplayUtils.dp2px(getContext(), checkBoxWidthDp));
    }

    public GYCheckBox setCheckBoxHeightPx(int checkBoxHeightPx) {
        checkBoxLL.getViewSet().setHeightPx(checkBoxHeightPx);
        return this;
    }

    public GYCheckBox setCheckBoxHeightDp(int checkBoxHeightDp) {
        return setCheckBoxHeightPx(GYDisplayUtils.dp2px(getContext(), checkBoxHeightDp));
    }

    public GYCheckBox setCheckBoxRadiusPx(int checkBoxRadiusPx) {
        checkBoxLL.getBackgroundBuilder().setRadiusPx(checkBoxRadiusPx).build();
        return this;
    }

    public GYCheckBox setCheckBoxRadiusDp(int checkBoxRadiusDp) {
        return setCheckBoxRadiusPx(GYDisplayUtils.dp2px(getContext(), checkBoxRadiusDp));
    }

    public GYCheckBox setCheckBoxEdgeWidthPx(int checkBoxEdgeWidthPx) {
        checkBoxLL.getBackgroundBuilder().setEdgeWidthPx(checkBoxEdgeWidthPx).build();
        return this;
    }

    public GYCheckBox setCheckBoxEdgeWidthDp(int checkBoxEdgeWidthDp) {
        return setCheckBoxEdgeWidthPx(GYDisplayUtils.dp2px(getContext(), checkBoxEdgeWidthDp));
    }

    public GYCheckBox setCheckBoxEdgeColor(int checkBoxEdgeColor) {
        checkBoxLL.getBackgroundBuilder().setEdgeColor(checkBoxEdgeColor).build();
        return this;
    }

    public GYCheckBox setChecked(boolean checked) {
        checkBox.setChecked(checked);
        return this;
    }

    public boolean isChecked() {
        return checkBox.isChecked();
    }

    public GYCheckBox setTextSizeSp(int textSizeSp) {
        setLeftTextSizeSp(textSizeSp);
        setTopTextSizeSp(textSizeSp);
        setRightTextSizeSp(textSizeSp);
        setBottomTextSizeSp(textSizeSp);
        return this;
    }

    public GYCheckBox setTextSizePx(int textSizePx) {
        setLeftTextSizePx(textSizePx);
        setTopTextSizePx(textSizePx);
        setRightTextSizePx(textSizePx);
        setBottomTextSizePx(textSizePx);
        return this;
    }

    public GYCheckBox setTextColor(int textColor) {
        setLeftTextColor(textColor);
        setTopTextColor(textColor);
        setRightTextColor(textColor);
        setBottomTextColor(textColor);
        return this;
    }

    public GYCheckBox setSpacingPx(int spacingPx) {
        setLeftSpacingPx(spacingPx);
        setTopSpacingPx(spacingPx);
        setRightSpacingPx(spacingPx);
        setBottomSpacingPx(spacingPx);
        return this;
    }

    public GYCheckBox setSpacingDp(int spacingDp) {
        setLeftSpacingDp(spacingDp);
        setTopSpacingDp(spacingDp);
        setRightSpacingDp(spacingDp);
        setBottomSpacingDp(spacingDp);
        return this;
    }

    public GYTextView getLeftTextView() {
        return leftTextView;
    }

    public GYCheckBox setLeftText(String leftText) {
        leftTextView.getViewSet().setVisibility(TextUtils.isEmpty(leftText) ? GONE : VISIBLE).setText(leftText);
        return this;
    }

    public String getLeftTextString() {
        return leftTextView.getViewSet().getTextString();
    }

    public GYCheckBox setLeftTextSizeSp(int leftTextSizeSp) {
        leftTextView.getViewSet().setTextSizeSp(leftTextSizeSp);
        return this;
    }

    public GYCheckBox setLeftTextSizePx(int leftTextSizePx) {
        return setLeftTextSizeSp(GYDisplayUtils.px2sp(getContext(), leftTextSizePx));
    }

    public GYCheckBox setLeftTextColor(int leftTextColor) {
        leftTextView.getViewSet().setTextColor(leftTextColor);
        return this;
    }

    public GYCheckBox setLeftTextBold(boolean leftTextBold) {
        leftTextView.getViewSet().setBold(leftTextBold);
        return this;
    }

    public GYCheckBox setLeftSpacingPx(int leftSpacingPx) {
        leftTextView.getViewSet().setMarginPx(0, 0, leftSpacingPx, 0);
        return this;
    }

    public GYCheckBox setLeftSpacingDp(int leftSpacingDp) {
        return setLeftSpacingPx(GYDisplayUtils.dp2px(getContext(), leftSpacingDp));
    }

    public GYTextView getTopTextView() {
        return topTextView;
    }

    public GYCheckBox setTopText(String topText) {
        topTextView.getViewSet().setVisibility(TextUtils.isEmpty(topText) ? GONE : VISIBLE);
        topTextView.setText(topText);
        return this;
    }

    public String getTopTextString() {
        return topTextView.getViewSet().getTextString();
    }

    public GYCheckBox setTopTextSizeSp(int topTextSizeSp) {
        topTextView.getViewSet().setTextSizeSp(topTextSizeSp);
        return this;
    }

    public GYCheckBox setTopTextSizePx(int topTextSizePx) {
        return setTopTextSizeSp(GYDisplayUtils.px2sp(getContext(), topTextSizePx));
    }

    public GYCheckBox setTopTextColor(int topTextColor) {
        topTextView.getViewSet().setTextColor(topTextColor);
        return this;
    }

    public GYCheckBox setTopTextBold(boolean topTextBold) {
        topTextView.getViewSet().setBold(topTextBold);
        return this;
    }

    public GYCheckBox setTopSpacingPx(int topSpacingPx) {
        topTextView.getViewSet().setMarginPx(0, 0, 0, topSpacingPx);
        return this;
    }

    public GYCheckBox setTopSpacingDp(int topSpacingDp) {
        return setTopSpacingPx(GYDisplayUtils.dp2px(getContext(), topSpacingDp));
    }

    public GYTextView getRightTextView() {
        return rightTextView;
    }

    public GYCheckBox setRightText(String rightText) {
        rightTextView.getViewSet().setVisibility(TextUtils.isEmpty(rightText) ? GONE : VISIBLE);
        rightTextView.setText(rightText);
        return this;
    }

    public String getRightTextString() {
        return rightTextView.getViewSet().getTextString();
    }

    public GYCheckBox setRightTextSizeSp(int rightTextSizeSp) {
        rightTextView.getViewSet().setTextSizeSp(rightTextSizeSp);
        return this;
    }

    public GYCheckBox setRightTextSizePx(int rightTextSizePx) {
        return setRightTextSizeSp(GYDisplayUtils.px2sp(getContext(), rightTextSizePx));
    }

    public GYCheckBox setRightTextColor(int rightTextColor) {
        rightTextView.getViewSet().setTextColor(rightTextColor);
        return this;
    }

    public GYCheckBox setRightTextBold(boolean rightTextBold) {
        rightTextView.getViewSet().setBold(rightTextBold);
        return this;
    }

    public GYCheckBox setRightSpacingPx(int rightSpacingPx) {
        rightTextView.getViewSet().setMarginPx(rightSpacingPx, 0, 0, 0);
        return this;
    }

    public GYCheckBox setRightSpacingDp(int rightSpacingDp) {
        return setRightSpacingPx(GYDisplayUtils.dp2px(getContext(), rightSpacingDp));
    }

    public GYTextView getBottomTextView() {
        return bottomTextView;
    }

    public GYCheckBox setBottomText(String bottomText) {
        bottomTextView.getViewSet().setVisibility(TextUtils.isEmpty(bottomText) ? GONE : VISIBLE);
        bottomTextView.setText(bottomText);
        return this;
    }

    public String getBottomTextString() {
        return bottomTextView.getViewSet().getTextString();
    }

    public GYCheckBox setBottomTextSizeSp(int bottomTextSizeSp) {
        bottomTextView.getViewSet().setTextSizeSp(bottomTextSizeSp);
        return this;
    }

    public GYCheckBox setBottomTextSizePx(int bottomTextSizePx) {
        return setBottomTextSizeSp(GYDisplayUtils.px2sp(getContext(), bottomTextSizePx));
    }

    public GYCheckBox setBottomTextColor(int bottomTextColor) {
        bottomTextView.getViewSet().setTextColor(bottomTextColor);
        return this;
    }

    public GYCheckBox setBottomTextBold(boolean bottomTextBold) {
        bottomTextView.getViewSet().setBold(bottomTextBold);
        return this;
    }

    public GYCheckBox setBottomSpacingPx(int bottomSpacingPx) {
        bottomTextView.getViewSet().setMarginPx(0, bottomSpacingPx, 0, 0);
        return this;
    }

    public GYCheckBox setBottomSpacingDp(int bottomSpacingDp) {
        return setBottomSpacingPx(GYDisplayUtils.dp2px(getContext(), bottomSpacingDp));
    }

}
